<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: xinglou <xinglou1@foxmail.com>
 * +----------------------------------------------------------------------
 * 创建日期：2016-12-19
 * +----------------------------------------------------------------------
 * 文件描述：题项描述管理
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class QuestionDescAction extends CommonAction
{

    /**
     * 显示页面
     */
    public function index()
    {
        $model = new QuestionDescModel();
        
        // 条件检索
        if (! empty($_REQUEST['search_keyword'])) {
            $where['questionid|type|desc'] = array(
                'like',
                '%' . $this->_request('search_keyword') . '%'
            );
        }
        
        // 分页处理
        import('ORG.Util.Page'); // 导入分页类
        $count = $model->where($where)->count(); // 查询满足要求的总记录数
        $p = $this->_page($model, 'update_date', 'desc'); // 分页所需信息
        $page = new Page($count, $p['listRows']); // 实例化分页类 传入总记录数和每页显示的记录数
        $data = $model->field("id,
		                       	get_question_name(questionid) question,
		                        get_field_dict_name('question_desc', 'type', type) type,
								desc,
								get_user_name(operator) operator,
								update_date")
            ->where($where)
            ->order('update_date DESC, id DESC')
            ->limit($page->firstRow . ', ' . $page->listRows)
            ->select();
        $this->assign('list', $data);
        
        $show = $page->show(); // 分页显示输出
        $this->assign('show', $show); // 绑定分页显示字符串
        $this->display();
    }

    /**
     * 显示添加页面
     */
    public function add()
    {
        $model = new FormModel();
        $test_data = $model->field('id, 
									title, 
									intro, 
									item_info, 
									status')->select();
        $this->assign('test_data', $test_data);
        $formid = $this->_request('formid');
        if (empty($formid)) {
            $formid = $test_data[0]['id'];
        }
        $this->assign("formid", $formid);
        
        $formdata = $model->field('id, 
								   title, 
								   intro, 
								   item_info, 
								   status')
            ->where("id = '" . $formid . "'")
            ->find();
        if (! empty($formdata['item_info'])) {
            $item_info = explode(',', $formdata['item_info']);
            foreach ($item_info as $key => $value) {
                // 根据模块类型获取模块内容
                $item_info_detail = explode(':', $value);
                switch ($item_info_detail[0]) {
                    case 'select':
                        $data = $model->table("question_test_select")
                            ->field("'选择题' type,
									  		   code,
											   formid,
											   question,
											   score")
                            ->where("id = '" . $item_info_detail[1] . "'")
                            ->find();
                        break;
                    default:
                        $data = array();
                }
                if (! empty($data)) {
                    $data['item_type'] = $item_info_detail[0];
                    $data['item_id'] = $item_info_detail[1];
                    $item_info[$key] = $data;
                } else {
                    unset($item_info[$key]);
                }
            }
            $formdata['item_list'] = $item_info;
        }
        $this->assign('type_list', set_field_dict_list('question_desc', 'type'));
        $this->assign('formdata', $formdata);
        
        $this->display();
    }

    /**
     * 将添加的信息写入数据库
     */
    public function insert()
    {
        $model = new QuestionDescModel();
        $data = $model->create();
        if (false !== $data) {
            if (false !== $model->add()) {
                $this->success('添加成功', __APP__ . '?g=Admin&m=QuestionDesc&a=index');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 显示修改页面
     */
    public function edit()
    {
        $id = $this->_request('id');
        
        $model = new QuestionDescModel();
        $data = $model->field("id,
		                        questionid,
		                        formid,
		                        type,
								desc")
            ->where("id = '" . $id . "'")
            ->find();
        $this->assign('list', $data);
        $form_list = $this->get_form_list();
        $this->assign('form_list', $form_list);
        $question_test_select_list = $this->get_question_test_select_list();
        $this->assign('question_test_select_list', $question_test_select_list);
        $this->assign('type_list', set_field_dict_list('question_desc', 'type'));
        $this->display();
    }

    /**
     * 将修改的信息更新到数据库
     */
    public function update()
    {
        $model = new QuestionDescModel();
        $data = $model->create();
        // dump($data);
        if (false !== $data) {
            $data = $model->save();
            if (false !== $data) {
                $this->success('修改成功', __APP__ . '?g=Admin&m=QuestionDesc&a=index');
            } else {
                $this->error('修改失败');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 删除信息
     */
    public function delete()
    {
        $deleteId = $this->_request('ids');
        if (! empty($deleteId) && is_array($deleteId)) {
            $model = new QuestionDescModel();
            $id = implode("','", $deleteId); // 用','分割id数组，以便于拼装sql语句
            $id = "'" . $id . "'"; // 拼装sql语句，结果为'firstId','secondId',…
            if (false !== $model->where("id in(" . $id . ")")->delete()) {
                $this->success('删除成功', __APP__ . '?g=Admin&m=QuestionDesc&a=index');
            } else {
                $this->error('删除失败！');
            }
        } else {
            $this->error('您未选中需要删除的信息');
        }
    }
}
?>